<?php
//商品轮播图管理
namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests;

class SlideController extends CommonController
{
    //商品轮播图管理浏览
    public function index()
    {
		$db = \DB::table("m_slide");
		//判断并封装搜索条件
		$params = array();
		if(!empty($_GET['name'])){
			$db->where("name","like","%{$_GET['name']}%");
			$params['name'] = $_GET['name'];  //维持搜索条件
		}
		$data = $db->paginate(7);
		return view("admin.slide.index",['list'=>$data,'params'=>$params]);
    }
	//首页轮播图添加操作界面
    public function create()
    {
		$data = \DB::table("m_slide")->get();
		return view('admin.slide.add',['list'=>$data]);
    }
	//首页轮播图后台添加
    public function store(Request $request)
    {
		$data = $request->all();
		$data = $request->except('_token');
		$s = \DB::table("m_slide")->where('name',$data['name'])->first();
		if(!empty($s)){
			$info = "添加失败,该轮播图板块存在！！";
		}else{
			$id = \DB::table("m_slide")->insertGetId($data);
			$info = "添加轮播块成功！";
		}
		return redirect('admin/slide')->with("err",$info);
    }
	//单块轮播图管理浏览
    public function show($name)
    {		
		$data = \DB::table("m_slide")->where('name',$name)->first();
		return view("admin.slide.show",['slide'=>$data]);
    }
	//首页轮播图修改操作界面
    public function edit($id)
    {
		if(is_numeric($id)){
			$data = \DB::table("m_slide")->where('id',$id)->first();
			return view("admin.slide.edit",['list'=>$data]);
		}else{
			$data = \DB::table("m_slide")->where('name',$id)->first();
			if($data->sorder){
				\DB::table("m_slide")->where('name',$id)->update(['sorder' => !($data->sorder)]);
				$info = "成功关闭该轮播块！";
			}else{
				$n = \DB::table("m_slide")->where('sorder',1)->count();
				if($n < 10){
					\DB::table("m_slide")->where('name',$id)->update(['sorder' => !($data->sorder)]);
					$info = "成功开启该轮播块！";
				}else{
					$info = "轮播图已满，请先关闭在开启其它！";
				}
			}
			return redirect('admin/slide')->with("err",$info);
		}
    }
	//首页轮播图后台修改
    public function update(Request $request, $name)
    {
		$data =	$request->all();
		$data = $request->except('_token','_method');
		$pic1 = new \App\Org\Upload("pic1");
		$pic1->path="slide/"; 
        $pic1->typeList =array("image/jpeg","image/png","image/gif"); 
        $pic1->maxSize =0; 
        $res = $pic1->doUpload();
		$data['pic1'] = $res;
		$pic2 = new \App\Org\Upload("pic2");
		$pic2->path="slide/"; 
        $pic2->typeList =array("image/jpeg","image/png","image/gif"); 
        $pic2->maxSize =0; 
        $res = $pic2->doUpload();
		$data['pic2'] = $res;
		\DB::table('m_slide')->where('name',$name)->update($data);
		return redirect()->action('admin\SlideController@show',[$name]);
    }
	//首页轮播图后台删除
    public function destroy($id)
    {
		\DB::table('m_slide')->where('id', $id)->delete();
		$info = "首页轮播图删除成功！";
		return redirect('admin/slide')->with("err",$info);
    }
}
